//
//  HWXiuGaiEmailViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/10/30.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWXiuGaiEmailViewController.h"
#import "Countdown.h"
#import "HWGuanLianEmailViewController.h"
#import "HWChangePasswordViewController.h" // 修改密码
@interface HWXiuGaiEmailViewController ()
@property(strong,nonatomic)UILabel *nameLabel;
@property(strong,nonatomic)UIView *whiteView;
@property(strong,nonatomic)UILabel *describeLabel;
@property(strong,nonatomic)UITextField *titleTextField;
@property(strong,nonatomic)UIView *fenxianView;
@property(strong,nonatomic)UIButton *codeBut;
@property(strong,nonatomic)UIView *lowerPartView;
@property(strong,nonatomic) NSString *dataStr;

@end

@implementation HWXiuGaiEmailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
    self.nameLabel.text  = [NSString stringWithFormat:@"绑定邮箱：%@",self.emailStr];
    self.nameLabel.text  = [NSString stringWithFormat:@"%@：%@",[HWjianzhiduiObject keylocalization:@"绑定邮箱"],self.emailStr];
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
    
    
}

// 设置UI
- (void)setupUI {
     self.dataStr = @"";
    self.navigationItem.title = @"邮箱验证";
    self.navigationItem.title =  [HWjianzhiduiObject keylocalization:@"邮箱验证"];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-100)/2, CF_NavHeight+45, 100, 100)];
    imageView.image = [UIImage imageNamed:@"b_qq_email"];
    [self.view addSubview:imageView];
    
    
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, imageView.bottom+20, kScreenW, 14)];
    self.nameLabel.font = [UIFont systemFontOfSize:14];
    self.nameLabel.textColor = RGB(51, 51, 51);
    self.nameLabel.text = @"绑定邮箱：6464824@qq.com";
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
      // self.namelabel.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.nameLabel];
    
    self.whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, self.nameLabel.bottom+20, kScreenW, 44)];
    self.whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: self.whiteView];
    
      self.describeLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0,55, 44)];
        self.describeLabel.font = [UIFont systemFontOfSize:14];
        self.describeLabel.textColor = RGB(51, 51, 51);
        self.describeLabel.text = @"验证码";
    self.describeLabel.text =  [HWjianzhiduiObject keylocalization: @"验证码"];
    
    //  self.describeLabel.backgroundColor = [UIColor redColor];
    [self.whiteView  addSubview:self.describeLabel];
      
      self.titleTextField = [[UITextField alloc]initWithFrame:CGRectMake( self.describeLabel.right+1, 0, 300, 44)];
    
    NSString *shuru = [HWjianzhiduiObject keylocalization:@"请输入验证码"];
    
      NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:shuru attributes:
                 @{NSForegroundColorAttributeName:RGB(153, 153, 153),
                   NSFontAttributeName:[UIFont boldSystemFontOfSize:12]}
                 ];
      self.titleTextField.attributedPlaceholder  = attrString;
      self.titleTextField.font = [UIFont systemFontOfSize:12];
      self.titleTextField.textAlignment = NSTextAlignmentLeft;
      self.titleTextField.textColor = RGB(51, 51, 51);
      [self.whiteView addSubview:self.titleTextField];
      
      
      self.fenxianView = [[UIView alloc]initWithFrame:CGRectMake(0, 43, kScreenW, 1)];
      self.fenxianView.backgroundColor = xiatiaoyanSehui;
     [self.whiteView addSubview:self.fenxianView];
      
      
      
         UIButton *xianshyanj = [UIButton buttonWithType:UIButtonTypeCustom];
          xianshyanj.frame = CGRectMake(kScreenW-70-10,4, 70, 35);
    
    NSString *yanzheng =  [HWjianzhiduiObject keylocalization:@"获取验证码"];
          [xianshyanj setTitle:yanzheng forState:UIControlStateNormal];
          [xianshyanj setTitleColor:RGB(26, 113, 255) forState:UIControlStateNormal];
         // xianshyanj.titleLabel.textAlignment = NSTextAlignmentRight;
        //  xianshyanj.backgroundColor = [UIColor redColor];
          xianshyanj.titleLabel.font = [UIFont systemFontOfSize:11];
      //    [xianshyanj setBackgroundImage:[UIImage imageNamed:@"sign_surep"] forState:UIControlStateNormal];
      //    [xianshyanj setBackgroundImage:[UIImage imageNamed:@"kaijisign_surep_b"] forState:UIControlStateSelected];
          
     self.codeBut = xianshyanj;
     [self.whiteView  addSubview:xianshyanj];
          
     [xianshyanj addTarget:self action:@selector(xianshyanjTap:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    self.lowerPartView = [[UIView alloc]initWithFrame:CGRectMake(0,self.whiteView.bottom+30, kScreenW, 44)];
    // self.lowerPartView.backgroundColor = [UIColor yellowColor];
    self.lowerPartView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.lowerPartView];
    
    
    NSString *queding = [HWjianzhiduiObject keylocalization:@"确定"];
    
    UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(15, 0, kScreenW-15*2, 36) Title:queding Image:nil Font:13 Color:RGB(255, 255, 155) Target:self Selector:@selector(buttonTap)];
    butt.backgroundColor = blueBackgroundBT;
    butt.layer.cornerRadius = 18;
    butt.layer.masksToBounds = YES;
    butt.backgroundColor  = [UIColor bm_colorGradientChangeWithSizeView:butt];
    
    [self.lowerPartView addSubview:butt];
          
    
    
    
}

// 获取验证码
- (void)xianshyanjTap:(UIButton *)sende {
    
//    if (![WMUtil validateMobile:self.titleTextField.text]) {
//
//        [MBProgressHUD showError:@"请输入正确的手机号"];
//        return ;
//
//
//    }
//

    
    [Countdown  StartTimeCodeBtn:sende];
    
    [self youxiangyanzhengMa];
    
    
    
    
    
    
    
    
    
   
    
    
    
    DLog(@"获取验证码");
    

    
}

- (void)youxiangyanzhengMa {
    

    
    
    
    
    
    
    
    
     NSString *userID = [HWLocallyStoredData getLoginUserID];
     
     int t =  [WMUtil getRandomNumber:101 to:998];
     NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
     NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
     NSString *signMD5 = [WMUtil md5HexDigest:sign];
     NSDictionary *parametes = @{
                                @"ct" :  @"app",
                                @"ac" : @"user_email_codes",
                                @"ios" : @"1",
                                @"uid":userID,
                                @"t" :tStr,
                                @"sign" :signMD5,
                                @"member_id":[HWLocallyStoredData getCode],
                               
                                };
    [MBProgressHUD show:@"正在发送验证码"];
    [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {


        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
     //   [MBProgressHUD hideHUD];


        if ([codeStr intValue] == 1) {

            //  response[@"data"];

          //  [MBProgressHUD showSuccess:@"验证码发送成功"];

            [MBProgressHUD  showSuccess:response[@"message"]];
            self.dataStr =  [NSString stringWithFormat:@"%@",response[@"data"]];




        } else {
            //  self.dataStr= @"";
             [MBProgressHUD  showError:response[@"message"]];

        }
        NSLog(@"%@",response);


    } failure:^(NSString *error) {

        [MBProgressHUD  showError:@"验证码发送失败"];
       // self.dataStr= @"";

    }];
    
    
    
    
//    NSDictionary *parametes = @{
//                                       @"ct" :  @"app",
//                                       @"ac" : @"email_code",
//                                       @"ios" : @"1",
//                                       @"type" : @"1",
//                                       @"email": self.emailStr
//                                       };
//
//
//       [MBProgressHUD show:@"正在发送验证码"];
//       [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
//
//
//               NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
//            //   [MBProgressHUD hideHUD];
//
//
//               if ([codeStr intValue] == 1) {
//
//                   //  response[@"data"];
//
//                 //  [MBProgressHUD showSuccess:@"验证码发送成功"];
//
//                   [MBProgressHUD  showSuccess:response[@"message"]];
//                   self.dataStr =  [NSString stringWithFormat:@"%@",response[@"data"]];
//
//
//
//
//               } else {
//                   //  self.dataStr= @"";
//                    [MBProgressHUD  showError:response[@"message"]];
//
//               }
//               NSLog(@"%@",response);
//
//
//           } failure:^(NSString *error) {
//
//               [MBProgressHUD  showError:@"验证码发送失败"];
//              // self.dataStr= @"";
//
//           }];
//
//
    
    
    
    
    
    
    
    

//    NSDictionary *parametes = @{
//                                    @"ct" :  @"app",
//                                    @"ac" : @"email_code",
//                                    @"ios" : @"1",
//                                    @"type" : @"1",
//                                    @"email": self.emailStr
//                                    };
//
//
//    [MBProgressHUD show:@"正在发送验证码"];
//    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
//
//
//            NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
//         //   [MBProgressHUD hideHUD];
//
//
//            if ([codeStr intValue] == 1) {
//
//                //  response[@"data"];
//
//              //  [MBProgressHUD showSuccess:@"验证码发送成功"];
//
//                [MBProgressHUD  showSuccess:response[@"message"]];
//                self.dataStr =  [NSString stringWithFormat:@"%@",response[@"data"]];
//
//
//
//
//            } else {
//                //  self.dataStr= @"";
//                 [MBProgressHUD  showError:response[@"message"]];
//
//            }
//            NSLog(@"%@",response);
//
//
//        } failure:^(NSString *error) {
//
//            [MBProgressHUD  showError:@"验证码发送失败"];
//           // self.dataStr= @"";
//
//        }];

    
    
    
    
}












//
- (void)buttonTap {
    
      DLog(@"下一步");
    
    
//    HWAssociatedPhoneViewController *associated = [[HWAssociatedPhoneViewController alloc]init];
//    [self.navigationController pushViewController:associated animated:YES];
    
    
     if (!(self.titleTextField.text.length > 0)) {
          [MBProgressHUD showError:@"验证码不能为空"];
          return;
      }
      

    
      NSString *userID = [HWLocallyStoredData getLoginUserID];
      int t = [WMUtil getRandomNumber:101 to:998];
    
      NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
      NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
      NSString *signMD5 = [WMUtil md5HexDigest:sign];
      NSDictionary *parametes = @{
                                   @"ct" :  @"app",
                                   @"ac" : @"remove_email",
                                   @"ios" : @"1",
                                   @"uid" : userID,
                                   @"t" : tStr,
                                   @"sign" :signMD5,
                                   @"email": @"",
                                   @"code" :   self.titleTextField.text,
                                   @"sessionid" : self.dataStr,
        
                                   };
      
      
      
       
       [MBProgressHUD show:@"正在解绑"];
       
       [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
           
         
           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        //   [MBProgressHUD hideHUD];
           
           
           if ([codeStr intValue] == 1) {
             
         
              [MBProgressHUD  showSuccess:response[@"message"]];
               [self.navigationController popViewControllerAnimated:YES];
               
               
          
               
               
               

           } else {
            
                [MBProgressHUD  showSuccess:response[@"message"]];
               
           }
           NSLog(@"%@",response);
           
           
       } failure:^(NSString *error) {

           [MBProgressHUD  showError:@"修改成功"];

           
       }];
       
      
    
    
    
    
 
    
    
}




@end
